# HCI574 - HW1 (2011) - solutions

# Here are the solutions for HW1, any comments starting with CH are mine
# I realize that some of you may have misread the questions (like #6) or how to work
# with code and I'll be very forgiving of these issues for now.
# See the  HW1_solution_results.txt for the output of this .py file

# 0) Example:print out 3 - put the exercise number first in your print statements
print "0)", 3  # print the a 0) and the number 3. No spaces before the print!

# 1) Import the math module (math) and use dir() to list its functions (1 pt)
import math
print "1)", dir(math)

# 2) print out the internal help on the log10 math function (1 pt)
print "2)", help(math.log10)
# CH: both help() and dir() take only the names of functions (i.e. not dir(math.log10()))

# 3) using functions and values defined by the math module,
# calculate the square-root of the log10 of Pi (2 pts)
print "3)", math.sqrt(math.log10(math.pi))
# CH:functions, constants, objects, variables defined inside the math module must be called
#    with the math prefix (fully qualified)
#    example of nesting functions: the result of pi is 3.1415, the result of log10(3.15) is given to
#    sqrt(), i.e. the evaluation order is "from the inside out"!

# 4) print the result of the random() function from the random module (1 pt)
import random
print "4)", random.random()
# CH: random is both, the name of module and of a function within this module

# 5) in the documentation look up the minumum and maximum possible values
#    returned by the random() function (1 pt)
print "5) random returns", 0.0, "to", 1.0
# CH: http://docs.python.org/library/random.html:
#   Return the next random floating point number in the range [0.0, 1.0) 
#   Minor point: [0.0 means including 0.0 but 1.0) means NOT including 1.0
#                so strictly speaking you should never get a 1.0!
 
# 6) Why (what) is there a difference of dividing (/ operator) 3 by 2
#    vs dividing 3 by 2.0 ?(2 pts)
print "6) 3/2 performs a integer division (how often does 2 fully fit in 3? 1 time), 3/2.0 performs a floating point division  with result 1.5"

# 7)Make a list l (ell) with 3 elements, the number 1, the number 2.7 and the string
# "Monty Python", print the list (1 pt)
l = [1, 2.7, "Monty Python"]
print "7)", "l contains", l

# 8) print the number of elements stored in the list l (1 pt)

print "8) The list l contains", len(l) , "elements"

# 9) make a string s from the last element of l using the total number of elements 
# in l to get the index/offset of "last" (1 pt)
s = l[len(l)-1]   # CH: this would be equivalent to l[-1]
print "9) s contains", s  # CH: string s contains the last element of the list l

# 10) concatenate (glue together) the first and last letter of s (2 pts)
first_and_last = s[0], s[-1]
print "10) first and last letters of s are", first_and_last

# 11) Using slices of s (e.g. [2:4]), + and *, assemble and print "PyPyMon" (2 pts)
print "11) glued together slices", s[6:8] * 2 + s[0:3]   # CH: here * means repeat

# 12) Give me (at least) two ways of creating a dictionary from the following 
# key : value pairs: "name" : "Ames" and  "population" : 50731  (2 pts)

# Create dict1 one way
dict1 = {"name":"Ames","population":50731}   # create dict with 2 pairs
print "12a) First dict", dict1

# Create same dict another way but call it dict2
dict2 = {}   # Create empty dict
dict2["name"] = "Ames"  # add first key/value pair
dict2["population"] = 50731 # add 2. pair
print "12b) Second dict", dict2

# 13 ) Random turtle goto star (5 pts)
print "13) Random Turtle goto star"
# import the turtle module
import turtle
import random  # import random module so I can use it's random() function later.
               # I usually put all my imports together at the start of the code.

#set canvas size from lower left corner to upper right corner
# note: this is different from sceensize()
max_x = 100
max_y = 100
turtle.setworldcoordinates(-max_x, -max_y, max_x, max_y)
# look up setworldcoordinates() about these canvas boundaries 

# 0,0 is the center of the canvas
# your turtle can move from -max_x/-max_y to +max_x/+max_y
print "canvas x coordinates go from", -max_x, "to", max_x
print "canvas y coordinates go from", -max_y, "to", max_y

# make turtle go to a random x/y pair (within the min/max canvas coordinates)

# for n in range(10): means repeat the indented code 10 times
for n in range(10): # use a higher number for more repeats
    # make sure lines below are indented with 4 spaces, like this line!

    # make a random x and a random y coordinate (stay within canvas boundaries!)
    # my solution may be a bit tricky to understand: 
    # you know that random()'s smallest value is 0.0 and its largest value is 1.0
    # (OK, it's 0.99999... but let's call it 1.0 here :)
    # You want random_coord_x to range from -100 (left) to +100 (right), so if random()
    # were to be 0.5 then random_coord_x would end up at 0 (middle of screen).
    # First, I multiply random() with 200 (max_y * 2), which will result in a number
    # between 0 and 200; then I subtract 100 from this, resulting in a number between
    # -100 and +100, which is exactly within the -max_x and max_x canvas boundaries I
    # specified in setworldcoordinates
    random_coord_x = random.random() * max_x * 2 - max_x 
    random_coord_y = random.random() * max_y * 2 - max_y     

    # make turtle goto this random x/y coordinate
    print n, ": goto x/y", random_coord_x, ",", random_coord_y
    turtle.goto(random_coord_x, random_coord_y)
    
print "did", n+1, "random gotos" # post-loop: no indent

